package com.easyexcle.demo.easyExcel.util;


import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lkz
 * @Title: WeekDayGrouping
 * @Description: 给定一个时间段， 计算时间段内的每一周  只显示周一至周五，按照周分组，再按照年分组
 * @Date: 2023/5/20 16:43
 */

public class WeekDayGrouping {





    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2023, 5, 1);
        LocalDate endDate = LocalDate.of(2023, 5, 31);
        Map<Integer, List<LocalDate>> dateMap = groupDatesByWeek(startDate, endDate);
        for (Map.Entry<Integer, List<LocalDate>> entry : dateMap.entrySet()) {
            System.out.println("Week " + entry.getKey() + ": " + entry.getValue());
        }
    }

    public static Map<Integer, List<LocalDate>> groupDatesByWeek(LocalDate startDate, LocalDate endDate) {
        Map<Integer, List<LocalDate>> dateMap = new HashMap<>();
        int weekNum = 1;
        LocalDate date = startDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
        while (!date.isAfter(endDate)) {
            List<LocalDate> weekDates = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                if (!date.isAfter(endDate)) {
                    weekDates.add(date);
                    date = date.plusDays(1);
                }
            }
            dateMap.put(weekNum, weekDates);
            weekNum++;
        }
        return dateMap;
    }




}
